import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Content: Timer计时器
 * --------------------------------------------------
 * Java 定时器 Timer 源码分析和使用建议: https://juejin.cn/post/6844903535029534733
 */

public class java_1101_Timer {
    public static void main(String[] args) {

        // Timer计时器
        System.out.println("--------------------Timer计时器--------------------");

        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                // scheduledExecutionTime() 返回此任务最近开始执行的时间
                Date date = new Date(this.scheduledExecutionTime());
                System.out.println("timeTask run " + date);
            }
        };

        Thread thread = new Thread() {
            @Override
            public void run() {
                System.out.println("Thread!");
            }
        };

        // 从现在开始每间隔 1000 ms 计划执行一个任务
        timer.schedule(timerTask, 0, 1000);

    }
}
